package com.qd.thirdpart.service;

import com.qd.common.panda.constant.PandaConstant;
import com.qd.panda.service.park.CarParkService;
import com.qd.thirdpart.domain.ThirdJtParkSyncDTO;
import com.qd.thirdpart.model.ThirdJtParkSync;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjk
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ThirdPartSyncService {
    private final ThirdJtParkSyncService thirdJtParkSyncService;
    private final CarParkService carParkService;


    @Async
    @Transactional(rollbackFor = Exception.class)
    public void save(List<ThirdJtParkSyncDTO> list) {
        if (log.isDebugEnabled()) {
            log.debug("save");
        }
        List<ThirdJtParkSync> addList = new ArrayList<>();
        List<ThirdJtParkSync> updateList = new ArrayList<>();
        thirdJtParkSyncService.buildAddOrUpdate(list, addList, updateList);

//        Map<Integer, Integer> map = new LinkedHashMap<>(list.size());
//        int a = 0;
//        for (int i = 0; i < list.size(); i++) {
//            ThirdJtParkSyncDTO thirdJtParkSyncDTO = list.get(i);
//            if (thirdJtParkSyncDTO.isConvert()) {
//                map.put(a, i);
//                a++;
//            }
//        }
//
//        Map<Integer, ThirdJtParkSyncDTO> convertMap = new LinkedHashMap<>();
//        List<GeoPoint> convertList = new LinkedList<>();
//        int size = map.keySet().size();
//        int count = 20;
//        int k = 0;
//        for (int i = 0; i < size; i++) {
//            //存储在list中元素的下标
//            Integer subscript = map.get(i);
//            ThirdJtParkSyncDTO thirdJtParkSyncDTO = list.get(subscript);
//            GeoPoint geoPoint = new GeoPoint();
//            geoPoint.setLat(thirdJtParkSyncDTO.getLat());
//            geoPoint.setLng(thirdJtParkSyncDTO.getLng());
//            convertMap.put(k, thirdJtParkSyncDTO);
//            convertList.add(geoPoint);
//            k++;
//            if (i % count == 0) {
//                thirdJtParkSyncService.build(convertList, convertMap);
//                convertMap.clear();
//                convertList.clear();
//                k = 0;
//            }
//            if (i == size - 1 && !convertMap.isEmpty()) {
//                thirdJtParkSyncService.build(convertList, convertMap);
//                convertMap.clear();
//                convertList.clear();
//                k = 0;
//            }
//        }
//        convertList.clear();
//        convertMap.clear();
//        map.clear();

        if (!addList.isEmpty()) {
            thirdJtParkSyncService.saveBatch(addList);
        }
        if (!updateList.isEmpty()) {
            thirdJtParkSyncService.updateBatchIds(updateList);
        }
        log.info("添加数量:{},更新数量:{}", addList.size(), updateList.size());

        list.forEach(thirdJtParkSyncService::saveOrUpdateThirdJtParkToCarPark);
        list.forEach(thirdJtParkSyncDTO -> {
            //更新缓存
            int feeStatus;
            if(StringUtils.hasLength(thirdJtParkSyncDTO.getFreeStatus())){
                if (thirdJtParkSyncDTO.getFreeStatus().contains("紧张")) {
                    feeStatus = PandaConstant.FREE_STATUS_TENSION;
                } else if (thirdJtParkSyncDTO.getFreeStatus().contains("充足")) {
                    feeStatus = PandaConstant.FREE_STATUS_ENOUGH;
                } else {
                    feeStatus = PandaConstant.FREE_STATUS_FULL;
                }
                carParkService.updateToRedis(thirdJtParkSyncDTO.getParkNo(), feeStatus, thirdJtParkSyncDTO.getTotal());
            }
        });
        list.forEach(ThirdJtParkSyncDTO::freeData);
        list.clear();
        addList.forEach(ThirdJtParkSync::freeData);
        addList.clear();
        updateList.forEach(ThirdJtParkSync::freeData);
        updateList.clear();
    }
}
